//
//  ActivityDetailViewController.swift
//  LiuheCommunity
//
//  Created by 罗恒 on 2018/1/23.
//  Copyright © 2018年 luo. All rights reserved.
//

import UIKit

class ActivityDetailViewController: UIViewController,UIScrollViewDelegate{
    var acId:String?
    var scrollView = UIScrollView()
    
    var imageH:CGFloat = 200.0
    var peopleView:UIView?
    var peopleLable:UILabel?
    var upper:UILabel?
    var npeople:UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "活动详情"
        view.backgroundColor = UIColor.white
    
        
        scrollView.frame = CGRect(x: 0, y: 0, width: w, height: h)
        scrollView.contentSize = CGSize(width: w, height: h )
        scrollView.delegate = self
        self.view.addSubview(scrollView)

        
    }

    override func viewWillAppear(_ animated: Bool) {
        
        self.tabBarController?.hidesBottomBarWhenPushed = true;
        
        self.tabBarController?.tabBar.isHidden = true//隐藏tabbar
        
        self.automaticallyAdjustsScrollViewInsets = false//移除隐藏后留下的空白
        
        WebService.shareInstance.activityDetail(activityId: self.acId) { (isSuccess, par) in
            let w = self.view.frame.size.width
            let share = ShareInfo.shareInstance
            self.scrollView.contentSize = CGSize(width: w, height: (1.0 + CGFloat(share.imgsArray.count) ) * self.imageH+64 )
            if isSuccess == true{
                let model = ShareInfo.shareInstance.modAc
                let picURL = NSURL.init(string: (model?.imgMainUrl)!)
                let imageV = UIImageView.init(frame:  CGRect(x: 0, y: 64
                    , width: Int(w), height: Int(self.imageH) ))
                imageV.sd_setImage(with: picURL! as URL)
                self.scrollView.addSubview(imageV)
                
                self.peopleView = UIView.init(frame: CGRect(x: w-130, y: self.imageH-30, width: 130, height: 30))
//                设置背景透明,子控件不影响
                self.peopleView?.backgroundColor = UIColor.init(red: 120 / 255.0, green: 122 / 255.0, blue: 145 / 255.0, alpha: 0.5)
                imageV.addSubview(self.peopleView!)

                self.peopleLable = UILabel.init(frame: CGRect(x: 10, y: 0, width: 60, height: 30))
                self.peopleLable?.text = "已报名:"
                self.peopleLable?.font = UIFont.boldSystemFont(ofSize: 15.0)
                self.peopleLable?.textColor = UIColor.white
                self.peopleView?.addSubview(self.peopleLable!)
                
                self.upper = UILabel.init(frame: CGRect(x: 90, y: 0, width: 60, height: 30))
                self.upper?.text = model?.upperLimit
                self.upper?.textColor = UIColor.white
                let fuhao = UILabel.init(frame: CGRect(x: 80, y: 0, width: 10, height: 30))
                fuhao.text = "/"
                fuhao.textColor = UIColor.white
                self.npeople = UILabel.init(frame: CGRect(x: 70, y: 0, width: 60, height: 30))
                self.npeople?.textColor = UIColor.white
                let npeo = model!.nPeople
                let str = NSString.init(format: "%d", npeo!)
                self.npeople?.text = str as String
                self.peopleView?.addSubview(self.npeople!)
                self.peopleView?.addSubview(fuhao)
                self.peopleView?.addSubview(self.upper!)
                
                
                if (share.imgsArray != nil && share.imgsArray.count > 0){
                for n in 0...share.imgsArray!.count - 1{
                    let models = share.imgsArray[n] as? ImageModel
                    let url = NSURL.init(string: (models?.url!)!)
                    let imageVV = UIImageView.init(frame: CGRect(x: 0, y: self.imageH * CGFloat(n + 1)+64
                        , width: CGFloat(w), height: self.imageH ))
                    imageVV.sd_setImage(with: url! as URL)
                    self.scrollView.addSubview(imageVV)
                }
                }
            }else{
                
                let a = ""
                
                MBProgressHUD.showToast(to: self.view, withText: a)
                
            }
        }

    }
}

